import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 *
 *
 */

public class NodeList<E> implements List<E> {

    private Node<E> first;
    private Node<E> last;
    private  int counter ;

    private static class Node<E> {

        E item;
        Node<E> previous;
        Node<E> next;

        Node(Node<E> previous, E element, Node<E> next) {
            this.previous = previous;
            this.item = element;
            this.next = next;
        }

    }

    @Override
    public boolean add(E element) {
        if (first == null) {
            first = new Node<E>(null, element, null);
            last = first;
        } else {
            Node<E> node = new Node<E>(last, element, null);
            last.next = node;
            last = node;
        }
        counter ++ ;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        Node<E> node = first;
        for (; node != null;) {
            builder.append(node.item);
            builder.append(node.next != null ? "," : "");
            node = node.next;
        }
        builder.append("]");
        return builder.toString();
    }

    @Override
    public int size() {
        return counter;
    }

    @Override
    public boolean isEmpty() {
        return counter == 0 ;
    }

    @Override
    public boolean contains(Object o) {
        Node<E> node = first ;
        for( ; node != null ; ) {
            if( o.equals(node.item) ) {
                return true ;
            }
            node = node.next;
        }
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean remove(Object o) {
        Node<E> node = first ;
        for( ; node != null ; ) {
            if( o.equals(node.item) ) {
                if( node.previous == null ) {
                    node.next.previous = null ;
                    first = node.next ;
                }
                else if( node.next == null ) {
                    node.previous.next = null ;
                    last = node.previous ;
                }
                else {
                    node.previous.next = node.next ;
                    node.next.previous = node.previous ;
                }
                counter -- ;
                System.out.println( "删除成功");
                return true ;
            }
            node = node.next;
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        first = null ;
        last = null ;
        counter = 0 ;
    }

    @Override
    public E get(int index) {
        if( index < 0 || index >= counter ) {
            System.out.println( "无效下标" );
        } else {
            Node<E> node = first ;
            for( int i = 0 ; node != null ; i ++ ) {
                if( i == index ) {
                    return node.item ;
                }
                node = node.next;
            }
        }
        return null;
    }

    @Override
    public E set(int index, E element) {
        if( index < 0 || index >= counter ) {
            System.out.println( "无效下标" );
        } else {
            Node<E> node = first ;
            for( int i = 0 ; node != null ; i ++ ) {
                if( i == index ) {
                    node.item = element ;
                    return node.item ;
                }
                node = node.next;
            }
        }
        return null;
    }

    @Override
    public void add(int index, E element) {
        if( index < 0 || index >= counter ) {
            System.out.println( "无效下标" );
        } else {
            Node<E> node = first ;
            for( int i = 0 ; node != null ; i ++ ) {
                if( i == index ) {
                    if( i == 0 ) {
                        Node<E> temp = new Node<E>(null, element, node);
                        node.previous = temp ;
                        first = temp ;
                    } else {
                        Node<E> temp = new Node<E>(node.previous, element, node);
                        node.previous.next = temp ;
                        node.previous = temp ;
                    }
                    counter ++ ;
                    System.out.println( "添加成功");
                    break;
                }
                node = node.next;
            }
        }
    }

    @Override
    public E remove(int index) {
        Node<E> node = first ;
        if( index < 0 || index >= counter ) {
            System.out.println( "无效下标" );
        } else {
            for( int i = 0 ; node != null ; i ++ ) {
                if( i == index ) {
                    if( node.previous == null ) {
                        node.next.previous = null ;
                        first = node.next ;
                    }
                    else if( node.next == null ) {
                        node.previous.next = null ;
                        last = node.previous ;
                    }
                    else {
                        node.previous.next = node.next ;
                        node.next.previous = node.previous ;
                    }
                    counter -- ;
                    System.out.println( "删除成功");
                    break;
                }
                node = node.next;
            }
        }
        return node.item;
    }

    @Override
    public int indexOf(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        // TODO Auto-generated method stub
        return null;
    }
}
